#include "SeqList.h"

//采用顺序表的方式实现通讯录

//初始化顺序表
void InitSeqList(SL* ps)
{
	assert(ps);
	ps->arr = NULL;
	ps->size = ps->capacity = 0;
}

//检查顺序表空间是否够用并执行动态扩容操作
void SLCheck(SL* ps)
{
	assert(ps);
	if (ps->size == ps->capacity)//内存不够
	{
		//判断原有空间大小为0给100否则2倍的扩容
		int newcapacity = ps->capacity == 0 ? 100 : 2 * ps->capacity;
		SLData* tmp = (SLData*)realloc(ps->arr, newcapacity * sizeof(SLData));
		if (tmp == NULL)
		{
			//扩容失败报错并退出整个程序
			perror("realloc fail!");
			exit(1);
		}
		ps->arr = tmp;
		ps->capacity = newcapacity;
	}

}

//顺序表尾插
void SLPushBack(SL* ps,SLData x)
{
	SLCheck(ps);
	ps->arr[ps->size++] = x;
}

//顺序表删除
void SLPop(SL* ps,int x)
{
	assert(ps);
			for (int i = x; i < ps->size-1; i++)
			{
				ps->arr[i] = ps->arr[i + 1];
			}
			ps->size -= 1;
}

//顺序表销毁
void SLDestroy(SL* ps)
{
	assert(ps);
	ps->arr = NULL;
	ps->capacity = ps->size = 0;
}
